<?php
class Administrator_movie_model extends CI_Model {

    function __construct()
    {
        parent::__construct();
    }	
	
	/*Type Management*/
	function type_management_add()
	{
		$movie_type_name = $this->input->post('movie_type_name');
		$datetime = date('Y-m-d H:i:s');
		
		//Insert movie_type
		$data = array(
			'movie_type_name' => $movie_type_name,
			'update_by' => $this->session->userdata('username'),			
			'create_date' => $datetime,
			'update_date' => $datetime
		);
		$this->db->insert('movie_type',$data);
	}
	
	function type_management_edit($movie_type_id)
	{
		$movie_type_name = $this->input->post('movie_type_name');
		$datetime = date('Y-m-d H:i:s');
		
		//Update movie_type
		$data = array(
			'movie_type_name' => $movie_type_name,
			'update_by' => $this->session->userdata('username'),			
			'update_date' => $datetime
		);
		$this->db->update('movie_type',$data,array('movie_type_id' => $movie_type_id));				
	}	
	
	function type_management_delete()
	{
		$id = $this->input->post('id');
		foreach($id as $value):
			$tables = array('movie_type','movie_type_relate');
			$this->db->where('movie_type_id', $value);
			$this->db->delete($tables);			
		endforeach;			
	}

	/*Hilight Update*/
	function hilight_update()
	{
		$home_hilight = $this->input->post('home_hilight');
		$this->db->update('module_page',array('json' => json_encode($home_hilight)),array('page' => 'movie'));	
	}			
		
	/*Content Management*/	
	function content_management_add()
	{
		$is_publish = $this->input->post('is_publish');
		$movie_type_id = $this->input->post('movie_type_id');
		$media_type_id = $this->input->post('media_type_id');
		$movie_publish_date = $this->input->post('movie_publish_date');
		$is_video_clip = $this->input->post('is_video_clip');
		$is_show_new_released = $this->input->post('is_show_new_released');
		$letters = $this->input->post('letters');
		$movie_name = $this->input->post('movie_name');
		$movie_description = $this->input->post('movie_description');
		$movie_author = $this->input->post('movie_author');
		$movie_actor = $this->input->post('movie_actor');
		$movie_tag = $this->input->post('movie_tag');
		$uploadify_store = explode(',',$this->input->post('uploadify_store'));
		$uploadify_store2 = explode(',',$this->input->post('uploadify_store2'));
		$uploadify_store3 = explode(',',$this->input->post('uploadify_store3'));
		$uploadify_store4 = explode(',',$this->input->post('uploadify_store4'));
		$datetime = date('Y-m-d H:i:s');		
		
		//Insert movie
		$data = array(
			'media_type_id' => $media_type_id,
			'movie_publish_date' => convertDatetime($movie_publish_date),
			'is_publish' => $is_publish,
			'is_video_clip' => $is_video_clip,
			'is_show_new_released' => $is_show_new_released,
			'letters' => $letters,
			'update_by' => $this->session->userdata('username'),
			'create_date' => $datetime,
			'update_date' => $datetime
		);
		$this->db->insert('movie',$data);
		$movie_id = $this->db->insert_id();
		
		//Insert movie_info
		$data = array(
			'movie_id' => $movie_id,
			'movie_name' => $movie_name,
			'movie_description' => $movie_description,
			'movie_author' => $movie_author,
			'movie_actor' => $movie_actor,
			'movie_tag' => $movie_tag
		);
		$this->db->insert('movie_info',$data);	

		//Insert movie_image
		$dataImage = array();
		if(!empty($uploadify_store[0])) foreach($uploadify_store as $key => $value):
			//Resize Image								
			$data = imageResize(array(
				'id' => $movie_id,
				'source_image' => $this->temp_folder.$value,
				'new_path' => $this->upload_folder.'movie/images/',
				'dimension' => array(
					array(
						'width' => 120,
						'height' => 120
					)					
				)
			));			
			
			//delete temp
			@unlink($this->upload_folder.$value);
			
			$dataImage[] = array(
				'movie_id' => $movie_id,
				'movie_file_name' => $data->file_name,
				'movie_file_ext' => $data->file_ext
			);	
		endforeach;
		if(!empty($dataImage)) $this->db->insert_batch('movie_image',$dataImage);
				
		//Insert movie_file(1)
		$dataFile = array();
		if(!empty($uploadify_store2[0])) foreach($uploadify_store2 as $key => $value):
			$dateY = date('Y',strtotime($datetime)).'/';
			$dateM = date('m',strtotime($datetime)).'/';
			$dateD = date('d',strtotime($datetime)).'/';
			$source_image = $this->temp_folder.$value;
			$path_info= pathinfo($source_image);
			$new_path = $this->upload_folder.'movie/files/';
			@mkdir($new_path.$dateY);
			@mkdir($new_path.$dateY.$dateM);
			@mkdir($new_path.$dateY.$dateM.$dateD);
			$new_path = $new_path.$dateY.$dateM.$dateD;
			$file_name= md5($movie_id.rand());
			$new_image= $new_path.$file_name.'.'.$path_info['extension'];		
			while(file_exists(realpath($new_image))):
				$file_name = md5($movie_id.rand());
				$new_image = $new_path.$file_name.'.'.$path_info['extension'];
			endwhile;			
			@copy($source_image,$new_image); //Put File to original
			//delete temp
			@unlink($this->temp_folder.$value);
			
			$dataFile = array(
				'movie_file_name1' => $file_name,
				'movie_file_ext1' => $path_info['extension']
			);	
		endforeach;
		if(!empty($dataFile)) $this->db->update('movie_info',$dataFile,array('movie_id' => $movie_id));
		
		//Insert movie_file(2)
		$dataFile = array();
		if(!empty($uploadify_store3[0])) foreach($uploadify_store3 as $key => $value):
			$dateY = date('Y',strtotime($datetime)).'/';
			$dateM = date('m',strtotime($datetime)).'/';
			$dateD = date('d',strtotime($datetime)).'/';
			$source_image = $this->temp_folder.$value;
			$path_info= pathinfo($source_image);
			$new_path = $this->upload_folder.'movie/files/';
			@mkdir($new_path.$dateY);
			@mkdir($new_path.$dateY.$dateM);
			@mkdir($new_path.$dateY.$dateM.$dateD);
			$new_path = $new_path.$dateY.$dateM.$dateD;
			$file_name= md5($movie_id.rand());
			$new_image= $new_path.$file_name.'.'.$path_info['extension'];		
			while(file_exists(realpath($new_image))):
				$file_name = md5($movie_id.rand());
				$new_image = $new_path.$file_name.'.'.$path_info['extension'];
			endwhile;			
			@copy($source_image,$new_image); //Put File to original
			//delete temp
			@unlink($this->temp_folder.$value);
			
			$dataFile = array(
				'movie_file_name2' => $file_name,
				'movie_file_ext2' => $path_info['extension']
			);	
		endforeach;
		if(!empty($dataFile)) $this->db->update('movie_info',$dataFile,array('movie_id' => $movie_id));	
		
		//Insert movie_file(3)
		$dataFile = array();
		if(!empty($uploadify_store4[0])) foreach($uploadify_store4 as $key => $value):
			$dateY = date('Y',strtotime($datetime)).'/';
			$dateM = date('m',strtotime($datetime)).'/';
			$dateD = date('d',strtotime($datetime)).'/';
			$source_image = $this->temp_folder.$value;
			$path_info= pathinfo($source_image);
			$new_path = $this->upload_folder.'movie/files/';
			@mkdir($new_path.$dateY);
			@mkdir($new_path.$dateY.$dateM);
			@mkdir($new_path.$dateY.$dateM.$dateD);
			$new_path = $new_path.$dateY.$dateM.$dateD;
			$file_name= md5($movie_id.rand());
			$new_image= $new_path.$file_name.'.'.$path_info['extension'];		
			while(file_exists(realpath($new_image))):
				$file_name = md5($movie_id.rand());
				$new_image = $new_path.$file_name.'.'.$path_info['extension'];
			endwhile;			
			@copy($source_image,$new_image); //Put File to original
			//delete temp
			@unlink($this->temp_folder.$value);
			
			$dataFile = array(
				'movie_file_name3' => $file_name,
				'movie_file_ext3' => $path_info['extension']
			);	
		endforeach;
		if(!empty($dataFile)) $this->db->update('movie_info',$dataFile,array('movie_id' => $movie_id));					
		
		//Insert movie_type_relate
		$dataMovieType = array();
		if(!empty($movie_type_id)) foreach($movie_type_id as $key => $value):
			$dataMovieType[] = array(
				'movie_id' => $movie_id,
				'movie_type_id' => $value				
			);
		endforeach;
		if(!empty($dataMovieType)) $this->db->insert_batch('movie_type_relate',$dataMovieType);
		
		//Update Solr
		$this->load->library('solr');	
			
		$document = new Apache_Solr_Document();
		$document->id = 'movie_'.$movie_id;
		$document->movie_name_s = $movie_name;
		$document->movie_tag_s = $movie_tag;
		$document->last_modified = date("Y-m-d\TH:i:s\Z",strtotime($datetime));
		if(!empty($movie_publish_date)) $document->last_modified = date("Y-m-d\TH:i:s\Z",strtotime(convertDatetime($movie_publish_date)));
		if(!empty($dataImage)) $document->movie_image_name_s = $dataImage['movie_file_name'].'.'.$dataImage['movie_file_ext'];	
		
		if($this->solr->ping() && !empty($document)):
			$this->solr->addDocument($document);
			$this->solr->commit(); //commit to see the deletes and the document
			$this->solr->optimize(); //merges multiple segments into one
		endif;
		
		//Clear Memcache
		$this->memcache->delete('dataHomeMovieShowing');
		$this->memcache->delete('dataHomeMovieComing');
		$this->memcache->delete('dataHomeMovieClip');
		$this->memcache->delete('dataMovieShow');				
		$this->memcache->delete('dataMovieHit');				
		$this->memcache->delete('dataMovieClip');				
		$this->memcache->delete('dataMovieComingSoon');	
		
		//Redirect to Edit Page
		redirect('movie/content_management/edit/'.$movie_id);					
	}
	
	function content_management_edit($movie_id)
	{
		$is_publish = $this->input->post('is_publish');
		$movie_type_id = $this->input->post('movie_type_id');
		$media_type_id = $this->input->post('media_type_id');
		$movie_publish_date = $this->input->post('movie_publish_date');
		$is_video_clip = $this->input->post('is_video_clip');
		$is_show_new_released = $this->input->post('is_show_new_released');
		$letters = $this->input->post('letters');
		$movie_name = $this->input->post('movie_name');
		$movie_description = $this->input->post('movie_description');
		$movie_author = $this->input->post('movie_author');
		$movie_actor = $this->input->post('movie_actor');
		$movie_tag = $this->input->post('movie_tag');
		$uploadify_store = explode(',',$this->input->post('uploadify_store'));
		$uploadify_store2 = explode(',',$this->input->post('uploadify_store2'));
		$uploadify_store3 = explode(',',$this->input->post('uploadify_store3'));
		$uploadify_store4 = explode(',',$this->input->post('uploadify_store4'));
		$datetime = date('Y-m-d H:i:s');		
		
		//Insert movie
		$data = array(
			'media_type_id' => $media_type_id,
			'movie_publish_date' => convertDatetime($movie_publish_date),
			'is_publish' => $is_publish,
			'is_video_clip' => $is_video_clip,
			'is_show_new_released' => $is_show_new_released,
			'letters' => $letters,
			'update_by' => $this->session->userdata('username'),
			'update_date' => $datetime
		);
		$this->db->update('movie',$data,array('movie_id' => $movie_id));
		
		//Insert movie_info
		$data = array(
			'movie_name' => $movie_name,
			'movie_description' => $movie_description,
			'movie_author' => $movie_author,
			'movie_actor' => $movie_actor,
			'movie_tag' => $movie_tag
		);
		$this->db->update('movie_info',$data,array('movie_id' => $movie_id));	

		//Insert movie_image
		$dataImage = array();
		if(!empty($uploadify_store[0])) foreach($uploadify_store as $key => $value):
			//Resize Image
			$data = imageResize(array(
				'id' => $movie_id,
				'source_image' => $this->temp_folder.$value,
				'new_path' => $this->upload_folder.'movie/images/',
				'dimension' => array(
					array(
						'width' => 120,
						'height' => 120
					)				
				)
			));
			
			$dataImage[] = array(
				'movie_id' => $movie_id,
				'movie_file_name' => $data->file_name,
				'movie_file_ext' => $data->file_ext
			);	
		endforeach;
		if(!empty($dataImage)) $this->db->insert_batch('movie_image',$dataImage);
		
		//Load Data Info
		$dataInfo = $this->db->select('t.*,j.create_date')
		->join('movie as j','j.movie_id=t.movie_id','INNER')
		->get_where('movie_info as t',array('t.movie_id' => $movie_id),1)->row();		
				
		//Insert movie_file (1)
		$dataFile = array();
		if(!empty($uploadify_store2[0])) foreach($uploadify_store2 as $key => $value):
			$dateY = date('Y',strtotime($dataInfo->create_date)).'/';
			$dateM = date('m',strtotime($dataInfo->create_date)).'/';
			$dateD = date('d',strtotime($dataInfo->create_date)).'/';
			$source_image = $this->temp_folder.$value;
			$path_info= pathinfo($source_image);
			$new_path = $this->upload_folder.'movie/files/';
			@mkdir($new_path.$dateY);
			@mkdir($new_path.$dateY.$dateM);
			@mkdir($new_path.$dateY.$dateM.$dateD);
			$new_path = $new_path.$dateY.$dateM.$dateD;
			$file_name= md5($movie_id.rand());
			$new_image= $new_path.$file_name.'.'.$path_info['extension'];		
			while(file_exists(realpath($new_image))):
				$file_name = md5($movie_id.rand());
				$new_image = $new_path.$file_name.'.'.$path_info['extension'];
			endwhile;			
			@copy($source_image,$new_image); //Put File to original
			
			//delete temp		
			@unlink($this->upload_folder.'movie/files/'.$dateY.$dateM.$dateD.$dataInfo->movie_file_name1.'.'.$dataInfo->movie_file_ext1);
			@unlink($this->temp_folder.$value);
			
			$dataFile = array(
				'movie_file_name1' => $file_name,
				'movie_file_ext1' => $path_info['extension']
			);	
		endforeach;
		if(!empty($dataFile)) $this->db->update('movie_info',$dataFile,array('movie_id' => $movie_id));
		
		//Insert movie_file (2)
		$dataFile = array();
		if(!empty($uploadify_store3[0])) foreach($uploadify_store3 as $key => $value):
			$dateY = date('Y',strtotime($dataInfo->create_date)).'/';
			$dateM = date('m',strtotime($dataInfo->create_date)).'/';
			$dateD = date('d',strtotime($dataInfo->create_date)).'/';
			$source_image = $this->temp_folder.$value;
			$path_info= pathinfo($source_image);
			$new_path = $this->upload_folder.'movie/files/';
			@mkdir($new_path.$dateY);
			@mkdir($new_path.$dateY.$dateM);
			@mkdir($new_path.$dateY.$dateM.$dateD);
			$new_path = $new_path.$dateY.$dateM.$dateD;
			$file_name= md5($movie_id.rand());
			$new_image= $new_path.$file_name.'.'.$path_info['extension'];		
			while(file_exists(realpath($new_image))):
				$file_name = md5($movie_id.rand());
				$new_image = $new_path.$file_name.'.'.$path_info['extension'];
			endwhile;			
			@copy($source_image,$new_image); //Put File to original
			
			//delete file		
			@unlink($this->upload_folder.'movie/files/'.$dateY.$dateM.$dateD.$dataInfo->movie_file_name2.'.'.$dataInfo->movie_file_ext2);
			@unlink($this->temp_folder.$value);
			
			$dataFile = array(
				'movie_file_name2' => $file_name,
				'movie_file_ext2' => $path_info['extension']
			);	
		endforeach;
		if(!empty($dataFile)) $this->db->update('movie_info',$dataFile,array('movie_id' => $movie_id));
		
		//Insert movie_file (3)
		$dataFile = array();
		if(!empty($uploadify_store4[0])) foreach($uploadify_store4 as $key => $value):
			$dateY = date('Y',strtotime($dataInfo->create_date)).'/';
			$dateM = date('m',strtotime($dataInfo->create_date)).'/';
			$dateD = date('d',strtotime($dataInfo->create_date)).'/';
			$source_image = $this->temp_folder.$value;
			$path_info= pathinfo($source_image);
			$new_path = $this->upload_folder.'movie/files/';
			@mkdir($new_path.$dateY);
			@mkdir($new_path.$dateY.$dateM);
			@mkdir($new_path.$dateY.$dateM.$dateD);
			$new_path = $new_path.$dateY.$dateM.$dateD;
			$file_name= md5($movie_id.rand());
			$new_image= $new_path.$file_name.'.'.$path_info['extension'];		
			while(file_exists(realpath($new_image))):
				$file_name = md5($movie_id.rand());
				$new_image = $new_path.$file_name.'.'.$path_info['extension'];
			endwhile;
			@copy($source_image,$new_image); //Put File to original
			
			//delete file		
			@unlink($this->upload_folder.'movie/files/'.$dateY.$dateM.$dateD.$dataInfo->movie_file_name3.'.'.$dataInfo->movie_file_ext3);
			@unlink($this->temp_folder.$value);
			
			$dataFile = array(
				'movie_file_name3' => $file_name,
				'movie_file_ext3' => $path_info['extension']
			);	
		endforeach;
		if(!empty($dataFile)) $this->db->update('movie_info',$dataFile,array('movie_id' => $movie_id));			

		//Delete movie_song
		$this->db->delete('movie_type_relate',array('movie_id' => $movie_id));	
		//Insert movie_type_relate
		$dataMovieType = array();
		if(!empty($movie_type_id)) foreach($movie_type_id as $key => $value):
			$dataMovieType[] = array(
				'movie_id' => $movie_id,
				'movie_type_id' => $value				
			);
		endforeach;
		if(!empty($dataMovieType)) $this->db->insert_batch('movie_type_relate',$dataMovieType);
		
		//Update Solr
		$this->load->library('solr');	
			
		$document = new Apache_Solr_Document();
		$document->id = 'movie_'.$movie_id;
		$document->movie_name_s = $movie_name;
		$document->movie_tag_s = $movie_tag;
		$document->last_modified = date("Y-m-d\TH:i:s\Z",strtotime($datetime));
		if(!empty($movie_publish_date)) $document->last_modified = date("Y-m-d\TH:i:s\Z",strtotime(convertDatetime($movie_publish_date)));
		
		$dataImage = $this->db->select('movie_file_name,movie_file_ext')->order_by('movie_image_order ASC,movie_image_id ASC')->get_where('movie_image',array('movie_id' => $movie_id),1)->row();
		if(!empty($dataImage->movie_file_name)) $document->movie_image_name_s = $dataImage->movie_file_name.'.'.$dataImage->movie_file_ext;
		
		if($this->solr->ping() && !empty($document)):
			$this->solr->addDocument($document);
			$this->solr->commit(); //commit to see the deletes and the document
			$this->solr->optimize(); //merges multiple segments into one
		endif;
		
		//Clear Memcache
		$this->memcache->delete('dataHomeMovieShowing');
		$this->memcache->delete('dataHomeMovieComing');
		$this->memcache->delete('dataHomeMovieClip');
		$this->memcache->delete('dataMovieShow');				
		$this->memcache->delete('dataMovieHit');				
		$this->memcache->delete('dataMovieClip');				
		$this->memcache->delete('dataMovieComingSoon');							
	}		

	function ajax_update_image()
	{
		$movie_image_id = $this->input->post('movie_image_id');
		if(!empty($movie_image_id)) foreach($movie_image_id as $key => $value):
			$this->db->update('movie_image',array('movie_image_order' => $key),array('movie_image_id' => $value));
		endforeach;
		exit;
	}
	
	function ajax_delete_image($movie_image_id)
	{
		$dataImage = $this->db->get_where('movie_image',array('movie_image_id' => $movie_image_id),1)->row();
		$this->db->delete('movie_image',array('movie_image_id' => $movie_image_id));
		//Delete All Image
		$map = directory_map($this->upload_folder.'movie/images/', 1);
		foreach($map as $folder):
			@unlink($this->upload_folder.'movie/images/'.$folder.'/'.$dataImage->movie_file_name.'.'.$dataImage->movie_file_ext);
		endforeach;
		exit;
	}
	
	function ajax_delete_media($movie_id,$field = array())
	{
		$dataInfo = $this->db
		->join('movie_info as j','j.movie_id=t.movie_id','INNER')
		->get_where('movie as t',array('t.movie_id' => $movie_id),1)
		->row();
		$this->db->update('movie_info',array(''.$field[0].'' => '',''.$field[1].'' => ''),array('movie_id' => $movie_id));
		//Delete File
		$dateY = date('Y',strtotime($dataInfo->create_date)).'/';
		$dateM = date('m',strtotime($dataInfo->create_date)).'/';
		$dateD = date('d',strtotime($dataInfo->create_date)).'/';
		@unlink($this->upload_folder.'movie/files/'.$dateY.$dateM.$dateD.$dataInfo->{''.$field[0].''}.'.'.$dataInfo->{''.$field[1].''});	
		exit;
	}	
	
	function content_management_delete()
	{
		$id = $this->input->post('id');
		$map = directory_map($this->upload_folder.'movie/images/', 1);
		$ids = array();
		foreach($id as $value):
			$ids[] = "movie_".$value;
			$dataInfo = $this->db->select('t.*,j.create_date')
			->join('movie as j','j.movie_id=t.movie_id','INNER')
			->get_where('movie_info as t',array('t.movie_id' => $value))->row();
			$dataImage = $this->db->get_where('movie_image',array('movie_id' => $value))->result();
			$tables = array('movie','movie_info','movie_type_relate','movie_image','movie_click_stat');
			$this->db->where('movie_id', $value);
			$this->db->delete($tables);
			//Delete All Image
			foreach($map as $folder):
				foreach($dataImage as $items):
					@unlink($this->upload_folder.'movie/images/'.$folder.'/'.$items->movie_file_name.'.'.$items->movie_file_ext);
				endforeach;
			endforeach;
			//Delete File
			$dateY = date('Y',strtotime($dataInfo->create_date)).'/';
			$dateM = date('m',strtotime($dataInfo->create_date)).'/';
			$dateD = date('d',strtotime($dataInfo->create_date)).'/';			
			@unlink($this->upload_folder.'movie/files/'.$dateY.$dateM.$dateD.$dataInfo->movie_file_name1.'.'.$dataInfo->movie_file_ext1);
			@unlink($this->upload_folder.'movie/files/'.$dateY.$dateM.$dateD.$dataInfo->movie_file_name2.'.'.$dataInfo->movie_file_ext2);
			@unlink($this->upload_folder.'movie/files/'.$dateY.$dateM.$dateD.$dataInfo->movie_file_name3.'.'.$dataInfo->movie_file_ext3);
		endforeach;	
		
		//Update Solr
		$this->load->library('solr');	
		if($this->solr->ping() && !empty($ids)):
			$this->solr->deleteByMultipleIds($ids);
			$this->solr->commit(); //commit to see the deletes and the document
			$this->solr->optimize(); //merges multiple segments into one
		endif;	
		
		//Clear Memcache
		$this->memcache->delete('dataHomeMovieShowing');
		$this->memcache->delete('dataHomeMovieComing');
		$this->memcache->delete('dataHomeMovieClip');
		$this->memcache->delete('dataMovieShow');				
		$this->memcache->delete('dataMovieHit');				
		$this->memcache->delete('dataMovieClip');				
		$this->memcache->delete('dataMovieComingSoon');						
	}
}

?>